package cn.schoolwow.util.domain.query.instanceList;

import cn.schoolwow.util.module.query.instanceList.domain.InstanceListQueryOption;

import java.util.Arrays;
import java.util.Collection;

public class QueryInstanceList<T> {
    private InstanceListQueryOption<T> instanceQueryOption = new InstanceListQueryOption();

    /**创建实例列表查询*/
    public static <T> QueryInstanceList<T> newQuery(Collection<T> list){
        QueryInstanceList instanceQuery = new QueryInstanceList();
        instanceQuery.instanceQueryOption.collection = list;
        return instanceQuery;
    }

    /**是否去重*/
    public QueryInstanceList<T> distinct(){
        instanceQueryOption.distinct = true;
        return this;
    }

    /**指定单个值查询*/
    public QueryInstanceList<T> addSingleValueQuery(Object singleValue){
        instanceQueryOption.singleValue = singleValue;
        return this;
    }

    /**指定实例列表查询*/
    public QueryInstanceList<T> addSingleValueListQuery(Collection singleValueList){
        instanceQueryOption.singleValueList = singleValueList;
        return this;
    }

    /**添加字段值查询*/
    public QueryInstanceList<T> addQuery(String fieldName, Object value){
        instanceQueryOption.fieldValueMap.put(Arrays.asList(fieldName), value);
        return this;
    }

    /**添加字段值查询*/
    public QueryInstanceList<T> addQuery(Collection<String> fieldNames, String value){
        instanceQueryOption.fieldValueMap.put(fieldNames, value);
        return this;
    }

    /**添加字段列表查询*/
    public QueryInstanceList<T> addInQuery(String fieldName, Object[] valueArray){
        if(null!=valueArray&&valueArray.length>0){
            instanceQueryOption.fieldValueCollectionMap.put(Arrays.asList(fieldName), Arrays.asList(valueArray));
        }
        return this;
    }

    /**添加字段列表查询*/
    public QueryInstanceList<T> addInQuery(String fieldName, Collection valueCollection){
        if(null!=valueCollection&&!valueCollection.isEmpty()){
            instanceQueryOption.fieldValueCollectionMap.put(Arrays.asList(fieldName), valueCollection);
        }
        return this;
    }

    /**添加字段不在列表查询*/
    public QueryInstanceList<T> addNotInQuery(String fieldName, Collection valueCollection){
        if(null!=valueCollection&&!valueCollection.isEmpty()){
            instanceQueryOption.fieldValueNotInCollectionMap.put(Arrays.asList(fieldName), valueCollection);
        }
        return this;
    }

    /**添加字段列表查询*/
    public QueryInstanceList<T> addInQuery(Collection<String> fieldNames, Collection valueCollection){
        if(null!=valueCollection&&!valueCollection.isEmpty()){
            instanceQueryOption.fieldValueCollectionMap.put(fieldNames, valueCollection);
        }
        return this;
    }

    /**添加字段不在列表查询*/
    public QueryInstanceList<T> addNotInQuery(Collection<String> fieldNames, Collection valueListvalueCollection){
        if(null!=valueListvalueCollection&&!valueListvalueCollection.isEmpty()){
            instanceQueryOption.fieldValueNotInCollectionMap.put(fieldNames, valueListvalueCollection);
        }
        return this;
    }

    /**指定升序排序字段*/
    public QueryInstanceList<T> orderByAsc(String fieldName){
        instanceQueryOption.orderByAscFieldName = fieldName;
        return this;
    }

    /**指定降序排序字段*/
    public QueryInstanceList<T> orderByDesc(String fieldName){
        instanceQueryOption.orderByDescFieldName = fieldName;
        return this;
    }

    public QueryInstanceListResponse<T> execute(){
        return new QueryInstanceListResponse(instanceQueryOption);
    }
}
